package com.emar.adx.yiqifa.util;

import org.apache.commons.compress.archivers.zip.Zip64Mode;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
import org.apache.commons.compress.archivers.zip.ZipFile;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.*;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

/**
 * Created by xiaoliang on 2015/4/29 14:40.
 */
public class ZipUtil {

    private static final Log log = LogFactory.getLog(ZipUtil.class);

    /**
     * 获取zip文件中的文件名
     *
     * @param zipFilePath zip文件路径
     */
    public static List<String> readZip(String zipFilePath) {

        File file = new File(zipFilePath);
        InputStream inputStream;
        StringBuffer sb = new StringBuffer();
        List<String> list = new ArrayList<String>();
        try {
            ZipFile zipFile = new ZipFile(file);
            Enumeration<ZipArchiveEntry> enties = zipFile.getEntries();
            ZipArchiveEntry entry;

            while (enties.hasMoreElements()) {
                entry = enties.nextElement();
//                sb.append(entry.getName()+";");
                list.add(entry.getName());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return list;
    }

    /**
     * 解压zip文件
     *
     * @param zipFilePath   zip文件路径
     * @param unzipFilePath 解压文件路径
     */
    public static void unZip(String zipFilePath, String unzipFilePath) {

        File file = new File(zipFilePath);
        InputStream inputStream;
        try {

            ZipFile zipFile = new ZipFile(file);
            Enumeration<ZipArchiveEntry> enties = zipFile.getEntries();
            ZipArchiveEntry entry;

            while (enties.hasMoreElements()) {
                entry = enties.nextElement();
                InputStream in = zipFile.getInputStream(entry);
                BufferedInputStream Bin = new BufferedInputStream(in);

                FileOutputStream out = new FileOutputStream(new File(unzipFilePath + entry.getName()));
                BufferedOutputStream Bout = new BufferedOutputStream(out);
                int b;
                while ((b = Bin.read()) != -1) {
                    Bout.write(b);
                }
                Bout.close();
                out.close();
                Bin.close();
                in.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 把文件压缩成zip格式
     *
     * @param files       需要压缩的文件
     * @param zipFilePath 压缩后的zip文件路径   ,如"D:/test/aa.zip";
     */
    public static void compressFilesToZip(File[] files, String zipFilePath) {
        if (files != null && files.length > 0) {
            ZipArchiveOutputStream zaos = null;
            try {
                File zipFile = new File(zipFilePath);
                zaos = new ZipArchiveOutputStream(zipFile);
                //Use Zip64 extensions for all entries where they are required
                zaos.setUseZip64(Zip64Mode.AsNeeded);

                //将每个文件用ZipArchiveEntry封装
                //再用ZipArchiveOutputStream写到压缩文件中
                for (File file : files) {
                    if (file != null) {
                        ZipArchiveEntry zipArchiveEntry = new ZipArchiveEntry(file, file.getName());
                        zaos.putArchiveEntry(zipArchiveEntry);
                        InputStream is = null;
                        try {
                            is = new BufferedInputStream(new FileInputStream(file));
                            byte[] buffer = new byte[1024 * 5];
                            int len = -1;
                            while ((len = is.read(buffer)) != -1) {
                                //把缓冲区的字节写入到ZipArchiveEntry
                                zaos.write(buffer, 0, len);
                            }
                            //Writes all necessary data for this entry.
                            zaos.closeArchiveEntry();
                        } catch (Exception e) {
                            throw new RuntimeException(e);
                        } finally {
                            if (is != null)
                                is.close();
                        }

                    }
                }
                zaos.finish();
            } catch (Exception e) {
                throw new RuntimeException(e);
            } finally {
                try {
                    if (zaos != null) {
                        zaos.close();
                    }
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }

        }


    }


    /**
     * 把文件压缩成zip格式
     *
     * @param inputStream       需要压缩的文件
     * @param zipFilePath 压缩后的zip文件路径   ,如"D:/test/aa.zip";
     */
//    public static void compressFilesToZip2(InputStream inputStream, String zipFilePath) {
//        if (inputStream != null) {
//            ZipArchiveOutputStream zaos = null;
//            try {
//                File zipFile = new File(zipFilePath);
//                zaos = new ZipArchiveOutputStream(zipFile);
//                //Use Zip64 extensions for all entries where they are required
//                zaos.setUseZip64(Zip64Mode.AsNeeded);
//
//                //将每个文件用ZipArchiveEntry封装
//                //再用ZipArchiveOutputStream写到压缩文件中
//                        ZipArchiveEntry zipArchiveEntry = new ZipArchiveEntry(file, file.getName());
//                        zaos.putArchiveEntry(zipArchiveEntry);
//                        InputStream is = null;
//                        try {
//                            is = new BufferedInputStream(new FileInputStream(file));
//                            byte[] buffer = new byte[1024 * 5];
//                            int len = -1;
//                            while ((len = is.read(buffer)) != -1) {
//                                //把缓冲区的字节写入到ZipArchiveEntry
//                                zaos.write(buffer, 0, len);
//                            }
//                            //Writes all necessary data for this entry.
//                            zaos.closeArchiveEntry();
//                        } catch (Exception e) {
//                            throw new RuntimeException(e);
//                        } finally {
//                            if (is != null)
//                                is.close();
//                        }
//
//                zaos.finish();
//            } catch (Exception e) {
//                throw new RuntimeException(e);
//            } finally {
//                try {
//                    if (zaos != null) {
//                        zaos.close();
//                    }
//                } catch (IOException e) {
//                    throw new RuntimeException(e);
//                }
//            }
//
//        }
//
//
//    }

}
